package com.aji.wineserver.controller;

import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.winecommon.result.PageResult;
import com.aji.winecommon.result.Result;
import com.aji.winecommon.utills.FileUtills;
import com.aji.winecommon.utills.SpectrumUtill;
import com.aji.winepojo.dto.WineDto;
import com.aji.winepojo.entity.ClassData;
import com.aji.winepojo.entity.IdentData;
import com.aji.winepojo.vo.WineVo;
import com.aji.winepojo.vo.WineVoPlus;
import com.aji.wineserver.config.FileEnvConfig;
import com.aji.wineserver.service.WineService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wine")
@Api(tags = "酒类样本数据管理")
@Slf4j
public class WineController {
    @Autowired
    private FileEnvConfig fileEnvConfig;
    @Autowired
    WineService wineService;
    @Autowired
    FileStorageService fileStorageService;
//    @ApiOperation("训练 模型")
//    @GetMapping("/trainModel")
//    private Result trainEle() throws IOException, InterruptedException {
//        elementService.trainEle();
//        return Result.success();
//    }
//
//    @ApiOperation("预测 根据id")
//    @GetMapping("/preModel/{ids}")
//    private ResponseEntity<byte[]> preEle(@PathVariable List<Integer> ids) throws IOException, InterruptedException {
//        ResponseEntity<byte[]> file = elementService.testEle(ids);
//        return file;
//    }
//    @PreAuthorize("hasAuthority('test')")
    @ApiOperation("查询 酒类样本，条件")
    @GetMapping
    public Result selectCon(WineDto wineDto) throws IOException {
        log.info("wine模糊查询controller:{},{},{}",wineDto.toString());
        List<WineVo> wineVoList = wineService.selectCon(wineDto);
        List<WineVoPlus> wineVoPlusList = new ArrayList<>();
        for (WineVo wineVo:wineVoList) {
            WineVoPlus wineVoPlus = new WineVoPlus();
            BeanUtils.copyProperties(wineVo,wineVoPlus);
            wineVoPlus.setSpectrumList(SpectrumUtill.strToList(wineVo.getSpectrum()));
            wineVoPlus.setSpectrumX(SpectrumUtill.getX(fileEnvConfig.getDataPath()+fileEnvConfig.getDemoFile()));
            wineVoPlusList.add(wineVoPlus);
        }
        return Result.success(wineVoPlusList);
    }

    @ApiOperation("查询 酒类样本，分页，条件")
    @GetMapping("/{page}/{pageSize}")
    public Result<PageResult> selectPage(WineDto wineDto, @PathVariable int page, @PathVariable int pageSize) throws IOException {
        log.info("wine模糊分页查询controller:{},{},{}",wineDto.toString(),page,pageSize);
        PageResult pageResult = wineService.pageQuery(wineDto,page,pageSize);
        return Result.success(pageResult);
    }
    @ApiOperation("查询 酒类光谱记录,根据id,用于页面回显")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable int id) throws IOException {
        WineVo wineVo = wineService.selectById(id);
        WineVoPlus wineVoPlus = new WineVoPlus();
        BeanUtils.copyProperties(wineVo,wineVoPlus);
        wineVoPlus.setSpectrumList(SpectrumUtill.strToList(wineVo.getSpectrum()));
        wineVoPlus.setSpectrumX(SpectrumUtill.getX(fileEnvConfig.getDataPath()+fileEnvConfig.getDemoFile()));
        return Result.success(wineVoPlus);
    }
    @ApiOperation("删除 酒类样本记录,批量")
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("wine删除controller:{}",ids.toString());
        wineService.deleteByIds(ids);
        return Result.success();
    }
    @ApiOperation("获取 demo文件")
    @GetMapping("/demoFile")
    public ResponseEntity<byte[]> getDemoFile() throws IOException {
        ResponseEntity<byte[]> file = FileUtills.transformFile(fileEnvConfig.getDataPath()+fileEnvConfig.getDemoFile(), fileEnvConfig.getDemoFile());
        return file;
    }
    @ApiOperation("增加 光谱数据")
    @PostMapping("/add")
    public Result add(@RequestParam("file")MultipartFile multipartFile){
        Integer i = wineService.add(multipartFile);
        return Result.success("已经成功添加"+i+"条数据");
    }
    @ApiOperation("修改 酒类光谱记录")
    @PutMapping("/{id}")
    public Result upDate(@PathVariable int id,@RequestParam(value = "file", required = false)MultipartFile multipartFile, Integer idenId, String pictureUrl){
        wineService.upDate(id,multipartFile,idenId, pictureUrl);
        return Result.success();
    }

    @ApiOperation("图片转url地址")
    @PostMapping("/imgToUrl")
    public Result imgToUrl(MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".jpg";
        File fileimg = File.createTempFile("temp", null);
        imgFile.transferTo(fileimg);
        FileInputStream simpJpgFile = MultipartFileUtill.multipartTifToJpgFileInputStream(fileimg);
        String accessSimpFileName = fileStorageService.uploadFile("img/jpg", simpFileName, simpJpgFile, "img/jpg");
        return Result.success(accessSimpFileName);
    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = wineService.count();
        return Result.success(count);
    }


//    @Autowired
//    FileStorageService fileStorageService;
//    @ApiOperation("MinIOtest")
//    @GetMapping("/minio")
//    public void minio() throws FileNotFoundException {
//        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\82527\\Pictures\\Saved Pictures\\preview.gif");
//        System.out.println(fileStorageService.uploadImgFile("","preview.gif",fileInputStream));
//    }
}
